package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.mapper.JcAjzxqkMapper;
import cn.com.css.taxInspection.attention.service.JcAjzxqkService;
import cn.com.css.taxInspection.gt3.entity.JcAjzxqkEntity;
import cn.com.css.taxInspection.gt3.service.DmGySwjgService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Slf4j
public class JcAjzxqkServiceImpl extends ServiceImpl<JcAjzxqkMapper, JcAjzxqkEntity> implements JcAjzxqkService {

    @Resource
    private DmGySwjgService dmGySwjgService;

    @Override
    @DS("oracle")
    public IPage<JcAjzxqkEntity> queryAjzxqkList(JcAjzxqkEntity jcAjzxqkEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        IPage<JcAjzxqkEntity> page = new Page<>(jcAjzxqkEntity.getPage(), jcAjzxqkEntity.getLimit());
        LambdaQueryWrapper<JcAjzxqkEntity> queryWrapper = Wrappers.lambdaQuery();
        //案件编号
        if (TsBqUtils.isNotNull(jcAjzxqkEntity.getJcajbh())) {
            queryWrapper.like(JcAjzxqkEntity::getJcajbh, jcAjzxqkEntity.getJcajbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(jcAjzxqkEntity.getNsrsbh())) {
            queryWrapper.like(JcAjzxqkEntity::getNsrsbh, jcAjzxqkEntity.getNsrsbh());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(jcAjzxqkEntity.getNsrmc())) {
            queryWrapper.like(JcAjzxqkEntity::getNsrmc, jcAjzxqkEntity.getNsrmc());
        }
        //立案日期
        if (TsBqUtils.isNotNull(jcAjzxqkEntity.getStartTime())) {
            queryWrapper.between(JcAjzxqkEntity::getLarq, sdf.parse(jcAjzxqkEntity.getStartTime()), sdf.parse(jcAjzxqkEntity.getEndTime()));
        }
        queryWrapper.orderByDesc(JcAjzxqkEntity::getJcajbh);
        IPage<JcAjzxqkEntity> iPage = this.page(page, queryWrapper);
        List<JcAjzxqkEntity> jcAjzxqkEntityList = iPage.getRecords();
        jcAjzxqkEntityList.forEach(entity -> {
            entity.setLabmmc(dmGySwjgService.getSwjgDmBySwjgJc(entity.getLabmDm()));
            entity.setZgswskfjmc(dmGySwjgService.getSwjgDmBySwjgJc(entity.getZgswskfjDm()));
            entity.setSkssswjgmc(dmGySwjgService.getSwjgDmBySwjgJc(entity.getSkssswjgDm()));
            entity.setZxbmmc(dmGySwjgService.getSwjgDmBySwjgJc(entity.getZxbmDm()));
        });
        return iPage;
    }
}
